var startTime = new Date().getTime();
var common = require('./common.js'),
	request = require('request'),
	FeedParser = require('feedparser'),
	rssSite = {},
	Iconv = require('iconv-lite'),
	fs = require('fs'),
	path = require('path'),
	grunt = require('grunt'),
	dir = "rss" + common.getTime(new Date(), "yyyyMMdd"),
	hasOwn = {}.hasOwnProperty,
	captcha = require('svg-captcha'),
	Segment = require('segment');

var cache = common.cache();

/*const redis = require('redis');
const redisClient = redis.createClient('6379', '192.168.31.133');

//获取当前db中所有的key
function getdbnamelist(callback){
    // 相当于命令（keys *）, 返回list，包含当前db所有key的名字
    redisClient.keys('*',function(err,val){
        callback && callback(val);
    });
}
 
 
//添加list数据
function insertListInDB(dbname,dbdata){
    redisClient.lpush(dbname,dbdata, function (err) {
        if (err) {
            console.log(err);
        }else{
            console.log('insert[%d] 个数据 in db[%s] finished',dbdata.length,dbname);
        }
    });
}
 
//查询指定的key中，指定位置的内容
function querylistdata(dbname, callback){
    //0 为起始位置，-1为最后的位置
    redisClient.lrange(dbname,0,-1,function(err,val){
       if(err){
           console.log(err);
       } else{
           console.log('read data from DB success. data length=[%d]',val.length);
           console.log(val)
			callback && callback(val)
       }
    });
}
 
 
//删除
function deletelistdata(dbname){
    //保留指定位置的内容，其他全部删除，所以从0到-1就是一个不删； 从-1到0就是数据全部删除，相当于del key
    redisClient.ltrim(dbname,-1,0,function(err,val){
        console.log('delet all data in db[%s] ，finished val=[%s] ',dbname,val);
    });    
}
 
//更新指定位置内容
function updatelist(dbname,dbdataindex,newinfo){
    redisClient.lset(dbname,dbdataindex,newinfo,function(err,val){
        console.log('update dbdataindex[%s] data.length=[%s],dbname=【%s】 finished val=[%s] ',dbdataindex,newinfo.length,dbname,val);
    });    
}*/


function route(handle, pathname) {
	console.log("About to route a request for " + pathname);
	if (typeof handle[pathname] === 'function') {
		handle[pathname]();
	} else {
		console.log("No request handler found for " + pathname);
	}
}

function index(config, res, query) {
	res.render('index', {}, function(err, html) {
		console.log(html)
		res.send(JSON.stringify({
			status: 1
		}));
	});
}

function err(res, type) {
	res.render(type, {});
}

function api_error(res, query, bool, msg) {
	var b = query.callback;
	res.send(b ? b + "(" + JSON.stringify({
		status: 0,
		data: [],
		msg: msg || "Unknown error"
	}) + ")" : {
		status: 0,
		data: [],
		msg: msg || "Unknown error"
	});
}

exports.route = route;
exports.index = index;
exports.err = err;

exports.channel = function(res, query) {
	getchannelnews(res, query);
}

exports.news = function(res, req) {
	getnewscontent(res, req);
}

exports.feed = function(res, query) {
	getnewsfeeds(res, query);
}

exports.config = function(res, query) {
	getsiteconfig(res, query);
}

exports.getpopdata = function(res, query) {
	getads(res, query);
}

exports.getTaobaoPopdata = function(res, query) {
	getTaobaoAds(res, query);
}

exports.submitinfo = function(res, query) {
	submitinfo(res, query);
}

exports.imgcaptcha = function(res, query, req) {
	imgcaptcha(res, query, req);
}

function imgcaptcha(res, query, req) {
	let cap = captcha.createMathExpr();
	req.session.captcha = cap.text; // session 存储
	res.type('svg'); // 响应的类型
	res.send(cap.data);
}

function submitinfo(res, query) {
	var query = req.body;
	console.log(query);
	if (query) {
		if (!query.user || query.user && query.user.replace(/\s+/gim, "") === "") {
			res.send(JSON.stringify({
				status: 0,
				msg: "user not empty."
			}))
			return;
		}
		if (query.user && !/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(query.user)) {
			res.send(JSON.stringify({
				status: 0,
				msg: "email formate error."
			}))
			return;
		}
		if (!query.password || query.password && query.password.replace(/\s+/gim, "") === "") {
			res.send(JSON.stringify({
				status: 0,
				msg: "password not empty."
			}))
			return;
		}
		if (query.password && !/^[A-Z]+[a-zA-Z0-9]+/.test(query.password)) {
			res.send(JSON.stringify({
				status: 0,
				msg: "password formate error."
			}))
			return;
		}
		if (query.password && query.password.length < 6) {
			res.send(JSON.stringify({
				status: 0,
				msg: "password length error."
			}))
			return;
		}
		var data = grunt.file.readJSON(path.resolve("user/__userdata.json")) || {
			users: {},
			length: 0
		};

		if (data && data.users) {
			if (data.users[query.user]) {
				var user = data.users[query.user];
				if (user.email != query.user) {
					res.send(JSON.stringify({
						status: 0,
						msg: "user name error."
					}));
					return;
				}
				if (common.md5(query.password) === user.password) {
					res.send(JSON.stringify({
						status: 0,
						msg: "password error."
					}));
					return;
				}
				req.session.signin = true;
				req.session.user = {
					id: user.id,
					name: user.name,
					email: user.email
				};
				res.cookie("userToken", encodeURIComponent("id=" + user.id + ",name=" + user.name + ",email=" + user.email + ",source=server"), {
					maxAge: 60 * 60 * 1000,
					path: '/',
					domain: '.iliulan.com'
				});

				res.send(JSON.stringify({
					status: 1,
					data: [{
						id: user.id,
						name: user.name,
						email: user.email
					}],
					msg: "signIn ok!"
				}));
			} else {
				req.session.signin = req.session.user = false;
				res.send(JSON.stringify({
					status: 0,
					msg: "no user error."
				}))
			}
		}
	} else {
		res.send(JSON.stringify({
			status: 0,
			msg: "error."
		}))
	}
}

function getads(res, query) {
	let b = query.callback,
		a = grunt.file.readJSON('user/ads.json');

	let data = a.data[type];
	resultData(res, data, b);
}

function getTaobaoAds(res, query) {
	let b = query.callback,
		type = query.type,
		a = grunt.file.readJSON('user/taobaoads.json');

	let data = a.data;
	resultData(res, data, b);
}

function getsiteconfig(res, query) {
	let b = query.callback,
		a = grunt.file.readJSON("user/site-config.json");

	resultData(res, [a.data], b)
}

function getnewsfeeds(res, query) {
	let b = query.callback,
		data = [];
	for (let i = 0; i < 3; i++) {
		let a = getchannelData(res, i, b);
		if (a !== false) {
			let sData = a.data && a.data.verify;
			sData = getListData(sData, 0, 5);
			data.push(sData);
		}
	}

	resultData(res, data, b);
}

function getchannelnews(res, req) {
	let query = req.body,
		b = query.callback,
		c = query.channel,
		l = parseFloat(query.count || 10),
		pn = parseFloat(query.pn || 0);

	let day = common.getTime(new Date(), "yyyyMMdd"),
		dir = "rss" + day,
		file = grunt.file.expand(path.resolve(dir + "/"+c+".json")),
		a = grunt.file.readJSON(file);

	if (a && a.data) {
		let data = a.data;
		data = getListData(data, pn, l, c, day);
		resultData(res, data, b)
	}else{
		noData(res, b)
	}
};

function getnewscontent(res, req) {
	let query = req.body,
		b = query.callback,
		c = query.channel,
		d = query.id,
		e = query.day;

	try{
		console.log(query)
		let day = e && e.split('-').join('') || common.getTime(new Date(), "yyyyMMdd"),
			dir = "rss" + day,
			file = grunt.file.expand(path.resolve(dir + "/"+c+".json"));
		console.log(file)
		let a = grunt.file.readJSON(file);
		if (a && a.data && a.data[d]){
			resultData(res, [common.extend(a.data[d], {
				type: c,
				day: day,
				newsid: d
			})], b);
		}else{
			noData(res, b)
		}
	}catch(e){
		noData(res, b)
	}



/*	let a = getchannelData(res, c, b);
	if (a !== false) {
		let data = a.data && a.data.verify;
		data = getNewsData(data, d);
		resultData(res, data, b);
	}*/
}

function getchannelData(res, name, b) {
	let a = {};
	switch (parseFloat(name)) {
		case 0:
			a = grunt.file.readJSON("user/institution.json");
			break;
		case 1:
			a = grunt.file.readJSON("user/individual.json");
			break;
		case 2:
			a = grunt.file.readJSON("user/training.json");
			break;
		default:
			noData(res, b);
			return false;
			break;
	}
	return a;
}

function getNewsData(data, id) {
	let result = [],
		len = data.length;

	for (let i = 0; i < len; i++) {
		if (data[i] && parseFloat(data[i].id) === parseFloat(id)) {
			result.push(data[i]);
			break;
		}
	}

	return result;
}

function getListData(data, pn, resultCount, c, day) {
	let len = data.length,
		start = (pn - 1 <= 0 ? 0 : pn - 1) * resultCount,
		end = start + resultCount,
		result = [];

	for (let i = start; i < end; i++) {
		data[i] && result.push(common.extend(data[i], {
			type: c,
			day: day,
			newsid: i
		}));
	}

	console.log("[getListData]: ", result.length, pn, resultCount, start, end)

	return result;
}

function noData(res, b) {
	res.send(b ? b + "(" + JSON.stringify({
		status: 0,
		data: [],
		total: 0
	}) + ")" : {
		status: 0,
		data: [],
		total: 0
	});
}

function resultData(res, content, b) {
	if (content.length > 0) {
		res.send(b ? b + "(" + JSON.stringify({
			status: 1,
			data: content,
			total: content.length
		}) + ")" : {
			status: 1,
			data: content,
			total: content.length
		});
	} else {
		noData(res, b)
	}
}

exports.countprops = function(res, query){
	var channel = query.channel,
		id = query.id,
		day = query.day,
		b = query.callback,
		a = grunt.file.readJSON("rss"+day.split('-').join('')+"/"+channel+".json");

	if (a && a.data && a.data[id]){
		common.extend(a.data[id], {
			countProps: a.data[id].countProps && a.data[id].countProps+1 || 1
		})

		grunt.file.write(path.resolve("rss"+day.split('-').join('')+"/"+channel+".json"), JSON.stringify(a));
		res.send(b ? b+"("+JSON.stringify({
			status:1
		})+")" : {
			status:1
		});
	}else{
		res.send({
			status:0
		});
	}
}


exports.getChannels = function(res, req) {
	var query = req.body,
		b = query.callback,
		name = query.name,
		channel = grunt.file.readJSON("user/rss.json");

	console.log(channel.data[name])

	res.send(b ? b + "(" + JSON.stringify(name ? {
		status: 1,
		data: channel.data[name]
	} : {
		status: 1,
		data: channel.list,
		total: channel.list.length || 0
	}) + ")" : name ? {
		status: 1,
		data: channel.data[name]
	} : {
		status: 1,
		data: channel.list,
		total: channel.list.length || 0
	});
}

function fetchKeys(data) {
	var keys_data = cache.get("keys") || {},
		item_keys = [],
		length = 0;
	for (var x = 0; x < data.length; x++) {
		var item = data[x];
		var keys = item.title.split(' ');

		for (var i = 0; i < keys.length; i++) {
			typeof keys_data[keys[i]] !== "undefined" ? (keys_data[keys[i]] += 1, keys_data[keys[i]] > 10 && item_keys.push(keys[i])) : (keys_data[keys[i]] = 1);
		}

	}
	if (grunt.file.exists(path.resolve("user/keys.json"))) {
		var file_keys = grunt.file.readJSON(path.resolve("user/keys.json"));
		if (file_keys && file_keys.data) {
			common.common.extend(keys_data, file_keys.data);
		}
		if (file_keys && typeof file_keys.length != "undefined") {
			length += file_keys.length;
		} else {
			length += 1;
		}
	}

	cache.commit({
		keys: keys_data
	});

	grunt.file.write(path.resolve("user/keys.json"), "{\"data\":" + common.stringify(keys_data) + ", \"length\":" + length + "}");
}

var otherServe = "http://47.93.99.52/channel?day={{day}}&channel={{channel}}&count={{count}}&pagenum={{pagenum}}&key={{key}}&type={{type}}&datatype={{datatype}}",
	otherserve_intervalTasks = "http://47.93.99.52/intervalTasks?type={{type}}",
	otherserve_addgood = "http://47.93.99.52/addgood?id={{id}}&type={{type}}&username={{username}}&userid={{userid}}",
	otherserve_delgood = "http://47.93.99.52/delgood?id={{id}}&type={{type}}&username={{username}}&userid={{userid}}",
	otherserve_search = "http://47.93.99.52/search?day={{day}}&key={{key}}&datatype={{datatype}}"



function fetchOtherServerJson(feed, from, callback) {
	request(feed, function(error, response, body) {
		if (!error && response.statusCode == 200) {
			/*var local_json = grunt.file.exists(path.resolve(dir + "/" + from + ".json")) && grunt.file.readJSON(path.resolve(dir + "/" + from + ".json")) || {
				data: []
			};*/
			var local_json = {
				data: []
			};
			var json = JSON.parse(body);
			//console.log(json.data)
			for (var i = 0; i < json.data.length; i++) {
				local_json.data.push(json.data[i]);
			}
			//json.data && grunt.file.write(path.resolve(dir + "/" + from + ".json"), "{\"data\":" + JSON.stringify(local_json.data) + "}");
			callback(json);
		}
	})
}

exports.intervalTasks = function(res, query) {
	var a = grunt.file.readJSON("user/rss.json"),
		b = query.callback,
		day = common.getTime(new Date(), "yyyyMMdd");
	dir = "rss" + day;
	for (var i in a.data) {
		/*if (a.data[i].region && a.data[i].region === "cn") {
			fetchOtherServerJson(common.tmpl(otherserve_intervalTasks, {
				type: i
			}), i, function(txt) {
				console.log("pull " + i);
			});
		} else {*/
			fetch(a.data[i].link, a.data[i].typeId, i, day, true, function(txt) {
				console.log("pull " + i);
			}, a.data[i].title);
		//}
	}
	res.send(b ? b + "(" + JSON.stringify({
		status: 1
	}) + ")" : {
		status: 1
	});
}

exports.intervalSTasks = function(res, req) {
	var a = grunt.file.readJSON("user/rss.json");
	var schedule = require('node-schedule'),
		day = common.getTime(new Date(), "yyyyMMdd");

	function scheduleCronstyle(v){
	    schedule.scheduleJob(v, function(){
	    	dir = "rss" + common.getTime(new Date(), "yyyyMMdd");
			for (var i in a.data) {
				fetch(a.data[i].link, a.data[i].typeId, i, day, true, function(txt) {
					console.log("pull " + i);
				}, a.data[i].title);
			}
			fetchBaiduHotSearchFn(res, req)
	        console.log('scheduleCronstyle:' + new Date());
	    }); 
	}

	scheduleCronstyle('0 30 10 * * *');
	scheduleCronstyle('0 30 14 * * *');
	scheduleCronstyle('0 30 18 * * *');
	scheduleCronstyle('0 30 22 * * *');
	scheduleCronstyle('0 30 02 * * *');
	scheduleCronstyle('0 30 06 * * *');
	res.send({
		status:1
	})
}

Array.prototype.filterArray = function(arr){
	var a = [];
	for (var i=0;i<arr.length;i++){
		this.indexOf(arr[i])<0 && a.push(arr[i]);
	}
	console.log(a)
	return a;
}

exports.search = function(res, req) {
	var query = req.body,
		username = query.username,
		userid = query.userid,
		email = query.email,
		size = 5;

	console.log(query)
	dir = "rss" + (query.day || common.getTime(new Date(), "yyyyMMdd"));
	var files = grunt.file.expand(path.resolve(dir + "/*.json"));
	//console.log(files, path.resolve(dir + "/*.json"))
	var k = query.key,
		content = [],
		b = query.callback;
	var seg = new Segment();
	seg.useDefault();
	k = k.replace(/\+|\./gim,'').split('%20').join('');
	var result = seg.doSegment(k, {
		simple: true
	});
	size = Math.round(files.length * 100 / result.length);
	console.log('size:', size)
	var r = [];
	for (var i=0;i<result.length;i++){
		result[i].length >= 2 && r.push(result[i])
	}

	k = r.length === 1 ? r[0] : r.join('|');

	if (username && userid && email){
		var XData = grunt.file.readJSON(path.resolve("user/__usersearchkey.json"));
		XData.users[email].keys = XData.users[email].keys.concat(XData.users[email].keys.filterArray(k.split('|')))
		grunt.file.write(path.resolve("user/__usersearchkey.json"), JSON.stringify(XData));
	}

	console.log(k)
	//for (var n = 0; n < result.length; n++) {
		for (var i = 0; i < files.length; i++) {
			var type = /\/([^\/\.]+)\.json/.exec(files[i]);
			//console.log(type[1])
			var json = grunt.file.readJSON(files[i]),
				count = 0;
			for (var x in json.data) {
				var reg = new RegExp("("+k+")", "gim");
				if (json.data[x] && (reg.test(json.data[x].description) || reg.test(json.data[x].title)) && (count+=1) && count <= size){
					var ctt = common.extend(json.data[x], {
						type: type[1] || "",
						region: "cn",
						newsid: x
					});
					content.push(ctt)
				}
			}
		}
	//}
	content.sort(function(a, b) {
		var c = new Date(a["pubDate"]).getTime();
		var d = new Date(b["pubDate"]).getTime();
		if (c < d) return 1
		else if (c > d) return -1
		else return 0
	});

	/*const CognitiveServicesCredentials = require('ms-rest-azure').CognitiveServicesCredentials;
	const WebSearchAPIClient = require('azure-cognitiveservices-websearch');
	let credentials = new CognitiveServicesCredentials('YOUR-ACCESS-KEY');
	let webSearchApiClient = new WebSearchAPIClient(credentials);

	webSearchApiClient.web.search('seahawks').then((result) => {
	    let properties = ["images", "webPages", "news", "videos"];
	    for (let i = 0; i < properties.length; i++) {
	        if (result[properties[i]]) {
	            console.log(result[properties[i]].value);
	        } else {
	            console.log(`No ${properties[i]} data`);
	        }
	    }
	}).catch((err) => {
	    throw err;
	})*/

	res.send(b ? b + "(" + JSON.stringify({
		status: 1,
		data: content,
		total: content.length,
		keywords: k.split('|')
	}) + ")" : {
		status: 1,
		data: content,
		total: content.length,
		keywords: k.split('|')
	});
};

exports.searchOther = function(res, query) {
	dir = "rss" + (query.day || common.getTime(new Date(), "yyyyMMdd"));

	fetchOtherServerJson(common.tmpl(otherserve_search, {
		day: query.day,
		key: encodeURIComponent(query.key),
		datatype: query.datatype
	}), i, function(result) {
		if (result.status === 1) {
			then(result);
		} else {
			then();
		}
	});

	function then(otherResult) {
		var files = grunt.file.expand(path.resolve(dir + "/*.json"));
		//console.log(files, path.resolve(dir + "/*.json"))
		var k = query.key,
			content = [],
			b = query.callback;
		var seg = new Segment();
		seg.useDefault();
		var result = seg.doSegment(k, {
			simple: true
		});
		for (var n = 0; n < result.length; n++) {
			k = result[n];
			for (var i = 0; i < files.length; i++) {
				var type = /\/([^\/\.]+)\.json/.exec(files[i]);
				//console.log(type[1])
				var json = grunt.file.readJSON(files[i]);
				for (var n in json.data) {
					var reg = new RegExp(k, "gim");
					(reg.test(json.data[n].description) || reg.test(json.data[n].title)) && content.push(common.extend(json.data[n], {
						type: type[1] || ""
					}));
				}
			}
		}
		if (otherResult) {
			for (var i = 0; i < otherResult.data.length; i++) {
				content.push(otherResult.data[i]);
			}
		}
		res.send(b ? b + "(" + JSON.stringify({
			status: 1,
			data: content,
			total: content.length,
			keywords: result
		}) + ")" : {
			status: 1,
			data: content,
			total: content.length,
			keywords: result
		});
	}
};

exports.recommendNews = function(res, req){
	var query = req.body,
		total = query.count;

	dir = "rss" + (query.day || common.getTime(new Date(), "yyyyMMdd"));
	var files = grunt.file.expand(path.resolve(dir + "/*.json"));
	var k = query.key,
		content = [],
		b = query.callback;

	for (var i = 0; i < files.length; i++) {
		var type = /\/([^\/\.]+)\.json/.exec(files[i]);
		var json = grunt.file.readJSON(files[i]),
			count = 0;
		for (var x in json.data) {
			if (json.data[x] && (count+=1) && count < total){
				var ctt = common.extend(json.data[x], {
					type: type[1] || "",
					region: "cn",
					newsid: x
				});
				content.push(ctt);
			}
		}
	}

	content.sort(function(a, b) {
		var c = new Date(a["pubDate"]).getTime();
		var d = new Date(b["pubDate"]).getTime();
		if (c < d) return 1
		else if (c > d) return -1
		else return 0
	});

	res.send(b ? b + "(" + JSON.stringify({
		status: 1,
		data: content,
		total: content.length
	}) + ")" : {
		status: 1,
		data: content,
		total: content.length
	});
}

exports.recommendKey = function(res, req) {
	var query = req.body,
		a = grunt.file.readJSON("user/recommend_key.json"),
		b = query.callback;

	res.send(b ? b + "(" + JSON.stringify({
		status: 1,
		data: a.data
	}) + ")" : {
		status: 1,
		data: a.data
	});

};

exports.beforeKeys = function(res, req) {
	var query = req.body,
		username = query.username,
		userid = query.userid,
		email = query.email,
		a = grunt.file.readJSON("user/__usersearchkey.json"),
		b = query.callback;

	var keys = [];

	if (a && a.users && a.users[email]){
		keys = a.users[email].keys;
		res.send(b ? b + "(" + JSON.stringify({
			status: 1,
			data: keys
		}) + ")" : {
			status: 1,
			data: keys
		});
	}else{
		res.send(b ? b + "(" + JSON.stringify({
			status: 0,
			data: keys
		}) + ")" : {
			status: 0,
			data: keys
		});
	}


};

exports.searchhotkeys = function(res, req) {
	var query = req.body,
		a = grunt.file.readJSON("tmp/baidusearchhotkeys.json"),
		b = query.callback;

	var keys = [];

	if (a && a.data){
		res.send(b ? b + "(" + JSON.stringify({
			status: 1,
			data: a.data
		}) + ")" : {
			status: 1,
			data: a.data
		});
	}else{
		res.send(b ? b + "(" + JSON.stringify({
			status: 0,
			data: keys
		}) + ")" : {
			status: 0,
			data: keys
		});
	}


};

exports.forgotpassword = function(res, req) {
	var query = req.body;
	//console.log(query);
	if (query) {
		if (!query.email || query.email && query.email.replace(/\s+/gim, "") === "") {
			res.send(JSON.stringify({
				status: 0,
				msg: "email not empty."
			}))
			return;
		}
		if (query.email && !/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(query.email)) {
			res.send(JSON.stringify({
				status: 0,
				msg: "email formate error."
			}))
			return;
		}
		var data = grunt.file.readJSON(path.resolve("user/__userdata.json")) || {
			users: {},
			length: 0
		};
		var email = query.email,
			key = "reset_" + (Math.random(10000) + "").replace(/\./gim, "");
		if (data.users && data.users[email]) {
			sendMail(email, data.users[email], key);
			res.cookie("userReset", encodeURIComponent("k=" + key + ",source=server"), {
				maxAge: 60 * 60 * 1000,
				path: '/',
				domain: '.iliulan.com'
			});
			res.send(JSON.stringify({
				status: 1,
				msg: "Send mail successfully."
			}))
		} else {
			res.send(JSON.stringify({
				status: 0,
				msg: "no email error."
			}))
		}
	} else {
		res.send(JSON.stringify({
			status: 0,
			msg: "error."
		}))
	}
};

exports.resetpassword = function(res, req) {
	var query = req.body;
	//console.log(query);
	if (query) {
		if (!query.email || query.email && query.email.replace(/\s+/gim, "") === "") {
			res.send(JSON.stringify({
				status: 0,
				msg: "email not empty."
			}))
			return;
		}
		if (query.email && !/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(query.email)) {
			res.send(JSON.stringify({
				status: 0,
				msg: "email formate error."
			}))
			return;
		}
		if (!query.password || query.password && query.password.replace(/\s+/gim, "") === "") {
			res.send(JSON.stringify({
				status: 0,
				msg: "password not empty."
			}))
			return;
		}
		if (query.password && !/^[A-Z]+[a-zA-Z0-9]+/.test(query.password)) {
			res.send(JSON.stringify({
				status: 0,
				msg: "password formate error."
			}))
			return;
		}
		if (query.password && query.password.length < 6) {
			res.send(JSON.stringify({
				status: 0,
				msg: "password length error."
			}))
			return;
		}
		var data = grunt.file.readJSON(path.resolve("user/__userdata.json")) || {
			users: {},
			length: 0
		};
		var email = query.email,
			password = query.password;
		if (data.users && data.users[email]) {
			data.users[email].password = common.md5(password);
			grunt.file.write(path.resolve(dir + "/__userdata.json"), JSON.stringify(data));
			res.cookie("userReset", "", {
				maxAge: -1000,
				path: '/',
				domain: '.iliulan.com'
			});
			res.send(JSON.stringify({
				status: 1,
				msg: "Reset password successfully."
			}))
		} else {
			res.send(JSON.stringify({
				status: 0,
				msg: "no email error."
			}))
		}
	} else {
		res.send(JSON.stringify({
			status: 0,
			msg: "error."
		}))
	}
};

exports.signin = function(res, req) {
	var query = req.body;
	//console.log(query);
	if (query) {
		if (!query.user || query.user && query.user.replace(/\s+/gim, "") === "") {
			res.send(JSON.stringify({
				status: 0,
				msg: "user not empty."
			}))
			return;
		}
		if (query.user && !/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(query.user)) {
			res.send(JSON.stringify({
				status: 0,
				msg: "email formate error."
			}))
			return;
		}
		if (!query.password || query.password && query.password.replace(/\s+/gim, "") === "") {
			res.send(JSON.stringify({
				status: 0,
				msg: "password not empty."
			}))
			return;
		}
		if (query.password && !/^[A-Z]+[a-zA-Z0-9]+/.test(query.password)) {
			res.send(JSON.stringify({
				status: 0,
				msg: "password formate error."
			}))
			return;
		}
		if (query.password && query.password.length < 6) {
			res.send(JSON.stringify({
				status: 0,
				msg: "password length error."
			}))
			return;
		}
		var data = grunt.file.readJSON(path.resolve("user/__userdata.json")) || {
			users: {},
			length: 0
		};

		if (data && data.users) {
			if (data.users[query.user]) {
				var user = data.users[query.user];
				if (user.email != query.user) {
					res.send(JSON.stringify({
						status: 0,
						msg: "user name error."
					}));
					return;
				}
				if (common.md5(query.password) === user.password) {
					res.send(JSON.stringify({
						status: 0,
						msg: "password error."
					}));
					return;
				}
				req.session.signin = true;
				req.session.user = {
					id: user.id,
					name: user.name,
					email: user.email
				};
				res.cookie("userToken", encodeURIComponent("id=" + user.id + ",name=" + user.name + ",email=" + user.email + ",source=server"), {
					maxAge: 60 * 60 * 1000,
					path: '/',
					domain: '.iliulan.com'
				});

				res.send(JSON.stringify({
					status: 1,
					data: [{
						id: user.id,
						name: user.name,
						email: user.email
					}],
					msg: "signIn ok!"
				}));
			} else {
				req.session.signin = req.session.user = false;
				res.send(JSON.stringify({
					status: 0,
					msg: "no user error."
				}))
			}
		}
	} else {
		res.send(JSON.stringify({
			status: 0,
			msg: "error."
		}))
	}
};

exports.signout = function(res, req) {
	var query = req.body;
	//console.log(query);
	if (query) {
		delete req.session.signin;
		delete req.session.user;
		res.cookie("userToken", '', {
			maxAge: -1000,
			path: '/',
			domain: '.iliulan.com'
		});
		res.send(JSON.stringify({
			status: 1,
			msg: "sign out ok."
		}))
	} else {
		res.send(JSON.stringify({
			status: 0,
			msg: "error."
		}))
	}
};

var count = 3000;
var original = []; //原始数组 
//给原始数组original赋值 
for (var i = 0; i < count; i++) {
	original[i] = i + 1;
}
original.sort(function() {
	return 0.5 - Math.random();
});

function getuserinfo(userid) {
	var a = grunt.file.readJSON("user/__userdata.json"),
		info = [];

	if (a && a.users) {
		userid = userid.split('|')
		for (var i = 0; i < userid.length; i++) {
			for (var n in a.users) {
				if (parseInt(a.users[n].id) === parseInt(userid[i])) {
					info.push({
						username: a.users[n].name,
						userid: a.users[n].id,
						userico: a.users[n].userico || "",
						email: a.users[n].email
					});
				}
			}
		}
	}
	return info;
}

exports.getuserinfo = function(res, req) {
	var a = grunt.file.readJSON("user/__userdata.json"),
		query = req.body,
		ids = query.ids,
		b = query.callback,
		info = [];

	if (a && a.users) {
		info = getuserinfo(ids);
		res.send(b ? b + "(" + JSON.stringify({
			status: 1,
			data: info
		}) + ")" : {
			status: 1,
			data: info
		});
	} else {
		api_error(res, query, false, "no user");
	}
};

exports.createaccount = function(res, req) {
	var query = req.body;
	//console.log(query);
	if (query) {
		/*if (req.session.signin) {
			res.send(JSON.stringify({
				status: 0,
				msg: "error"
			}));
			return;
		}*/
		if (!query.user || query.user && query.user.replace(/\s+/gim, "") === "") {
			res.send(JSON.stringify({
				status: 0,
				msg: "user not empty."
			}))
			return;
		}
		if (query.user && query.user.length < 6) {
			res.send(JSON.stringify({
				status: 0,
				msg: "user length error."
			}))
			return;
		}
		if (!query.email || query.email && query.email.replace(/\s+/gim, "") === "") {
			res.send(JSON.stringify({
				status: 0,
				msg: "email not empty."
			}))
			return;
		}
		if (query.email && !/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(query.email)) {
			res.send(JSON.stringify({
				status: 0,
				msg: "email formate error."
			}))
			return;
		}
		if (!query.password || query.password && query.password.replace(/\s+/gim, "") === "") {
			res.send(JSON.stringify({
				status: 0,
				msg: "password not empty."
			}))
			return;
		}
		if (query.password && !/^[A-Z]+[a-zA-Z0-9]+/.test(query.password)) {
			res.send(JSON.stringify({
				status: 0,
				msg: "password formate error."
			}))
			return;
		}
		if (query.password && query.password.length < 6) {
			res.send(JSON.stringify({
				status: 0,
				msg: "password length error."
			}))
			return;
		}
		if (query.email && hasUser(query.email)) {
			res.send(JSON.stringify({
				status: 0,
				msg: "The email address has been occupied."
			}))
			return;
		}
		/*if (!query.userico || query.userico && query.userico.replace(/\s+/gim, "") === "") {
			res.send(JSON.stringify({
				status: 0,
				msg: "your story not empty."
			}))
			return;
		}*/
		var name = query.user,
			password = query.password,
			email = query.email,
			//userico = query.userico,
			id = 0;
		var data = grunt.file.readJSON(path.resolve("user/__userdata.json")) || {
			users: {},
			length: 0
		};
		id = 10000+(data.length + 1);
		var userico = "";
		if (data.users && !data.users[email]) {
			var XData = JSON.stringify(common.extend(data, {
				users: common.extend(data.users, {
					[email]: {
						id: id,
						name: name,
						password: common.md5(query.password),
						email: email,
						userico: userico
					}
				}),
				length: data.length+1
			}));
			grunt.file.write(path.resolve("user/__userdata.json"), XData);
			XData = grunt.file.readJSON(path.resolve("user/__usergoods.json"));
			grunt.file.write(path.resolve("user/__usergoods.json"), {
				users: common.extend(XData.users, {
					[email]: {
						id: id,
						name: name,
						goodsArticle:[]
					}
				}),
				length: XData.length+1
			});
			XData = grunt.file.readJSON(path.resolve("user/__usermessage.json"));
			grunt.file.write(path.resolve("user/__usermessage.json"), {
				users: common.extend(XData.users, {
					[email]: {
						id: id,
						name: name,
						message:[]
					}
				}),
				length: XData.length+1
			});
			XData = grunt.file.readJSON(path.resolve("user/__usermessage.json"));
			grunt.file.write(path.resolve("user/__userfollows.json"), {
				users: common.extend(XData.users, {
					[email]: {
						id: id,
						name: name,
						follows:[],
						follower:[]
					}
				}),
				length: XData.length+1
			});
			XData = grunt.file.readJSON(path.resolve("user/__usersearchkey.json"));
			grunt.file.write(path.resolve("user/__usersearchkey.json"), {
				users: common.extend(XData.users, {
					[email]: {
						id: id,
						name: name,
						keys:[]
					}
				}),
				length: XData.length+1
			});
			res.cookie("userToken", encodeURIComponent("id=" + id + ",name=" + name + ",email=" + email + ",userico=" + userico + ",source=server"), {
				maxAge: 60 * 60 * 1000,
				path: '/',
				domain: '.iliulan.com'
			});
			sendMail(email, data.users[email], null, "Welcome to join us(iliulan.com)", "Hello, " + name + ". Welcome to iliulan.com. Here's your information:<br/>Login name: " + email + "<br/>Username: " + name + "<br/>Password: " + password + "<br/>Please keep in mind the above information. Please contact us if you have any questions.");
			res.send(JSON.stringify({
				status: 1,
				data: [{
					id: id,
					name: name,
					email: email,
					userico: userico
				}],
				msg: "create account ok!"
			}));
		} else {
			res.send(JSON.stringify({
				status: 0,
				msg: "The email address is occupied."
			}))
		}
	} else {
		res.send(JSON.stringify({
			status: 0,
			msg: "error."
		}))
	}
};

Array.prototype.delname = function(name, val) {
	var that = this;
	for (var i = 0; i < that.length; i++) {
		if (that[i][name] === val) {
			that.splice(i, 1);
			break;
		}
	}
	return that;
}

var hasUser = function(u, d) {
	var len = arguments.length;
	var user = grunt.file.readJSON(path.resolve("user/__userdata.json"));
	if (user && user.users) {
		if (len === 1) {
			return user.users[u] ? true : false;
		} else {
			var bool = false;
			for (var n in user.users) {
				if (user.users[n].name === u && parseFloat(user.users[n].id) === parseFloat(d)) {
					bool = true;
					break;
				}
			}
		}
		return bool;
	} else {
		return false;
	}
};

exports.delgood = function(res, req) {
	var rss = grunt.file.readJSON("user/rss.json"),
		query = req.body,
		c = query.type,
		b = query.callback,
		i = query.id,
		u = query.username,
		d = query.userid;

	var day = (query.day || common.getTime(new Date(), "yyyyMMdd"));
	console.log(day)
	/*if (rss.data && rss.data[c] && rss.data[c].region === "cn") {
		fetchOtherServerJson(common.tmpl(otherserve_delgood, {
			type: c,
			id: i,
			username: u,
			userid: d,
			day: day
		}), c, function(result) {
			fetchToUserFile(c, day, i, {}, u, d, true);
			res.send(b ? b + "(" + JSON.stringify({
				status: result.status,
				data: result.data
			}) + ")" : {
				status: result.status,
				data: result.data
			});
		});
	} else if (rss.data && rss.data[c] && rss.data[c].region === "en") {*/
		dir = "rss" + day;
		console.log(query, (new Date().getTime() - parseInt(query.key)) / 1000, 60);
		var filepath = path.resolve(dir + "/" + c + ".json");
		var iscity = false,
			city = "beijing";

		/*if (!grunt.file.exists(filepath)) {
			var pinyinlite = require('pinyinlite');
			city = pinyinlite(c).join('').split(',')[0];
			console.log(city)
			filepath = path.resolve(dir + "/baiducity_" + city + ".json");
			iscity = true;
		}*/

		var a = grunt.file.readJSON(filepath);
		if (a && a.data && a.data[i] && hasUser(u, d)) {
			if (a.data[i].goodusers) {
				a.data[i].goodusers = a.data[i].goodusers.delname("gooduser_name", u);
			} else {
				a.data[i].goodusers = [];
			}
			if (a.data[i].goodcount) {
				a.data[i].goodcount -= 1
			} else {
				a.data[i].goodcount = 0
			}
			common.extend(a.data[i], {
				goodcount: a.data[i].goodcount,
				goodusers: a.data[i].goodusers
			})

			/*if (!iscity) {
				if (cache.get(day)[c]) {
					console.log("default")
					cache.get(day)[c].data[i] = a.data[i]
				}
			} else {
				if (cache.get(day)["baiducity"] && cache.get(day)["baiducity"][city]) {
					console.log("city")
					cache.get(day)["baiducity"][city].data[i] = a.data[i];
				}
			}
			console.log(cache.get(day))*/

			grunt.file.write(filepath, JSON.stringify(a));
			fetchToUserFile(c, day, i, a.data[i], u, d, true);
			fetchToUserMessageFile(c, day, i, a.data[i], u, d, true,  " 取消点赞新闻 \"" + a.data[i].title + "\".");
			res.send(b ? b + "(" + JSON.stringify({
				status: 1,
				data: a.data[i]
			}) + ")" : {
				status: 1,
				data: a.data[i]
			});
		} else {
			res.send(b ? b + "(" + JSON.stringify({
				status: 0,
				data: []
			}) + ")" : {
				status: 0,
				data: []
			});
		}
	//}

};

function fetchToUserFile(channel, day, id, data, username, userid, bool) {
	var a = grunt.file.readJSON(path.resolve("user/__usergoods.json"));
	if (a && a.users) {
		for (var n in a.users) {
			if (a.users[n].name === username && parseInt(a.users[n].id) === parseInt(userid)) {
				if (!bool) {
					if (!a.users[n].goodsArticle) a.users[n].goodsArticle = [];
					a.users[n].goodsArticle.push(common.extend(data, {
						type: channel,
						day: day,
						newsid: id
					}));
					/*if (!a.users[n].message) a.users[n].message = [];
					a.users[n].message.push({
						message: username + " hit the news \"" + data.title + "\".",
						time: common.getTime(new Date(), "yyyy-MM-dd hh:mm:ss")
					});*/
					break;
				} else {
					if (!a.users[n].goodsArticle) {
						break;
					}
					var goods = a.users[n].goodsArticle;
					//console.log(goods)
					for (var i = 0; i < goods.length; i++) {
						if (goods[i].type === channel && goods[i].day === day && parseInt(goods[i].newsid) === parseInt(id)) {
							goods.splice(i, 1);
						}
					}
					a.users[n].goodsArticle = goods;
				}
			}
		}
		grunt.file.write(path.resolve("user/__usergoods.json"), JSON.stringify(a));
	}
}

function fetchToUserMessageFile(channel, day, id, data, username, userid, bool, msg) {
	var a = grunt.file.readJSON(path.resolve("user/__usermessage.json"));
	if (a && a.users) {
		for (var n in a.users) {
			if (a.users[n].name === username && parseInt(a.users[n].id) === parseInt(userid)) {
				if (!a.users[n].message) a.users[n].message = [];
				a.users[n].message.push({
					message: username + msg,
					time: common.getTime(new Date(), "yyyy-MM-dd hh:mm:ss"),
					aside: data
				});
				break;
			}
		}
		grunt.file.write(path.resolve("user/__usermessage.json"), JSON.stringify(a));
	}
}

exports.addgood = function(res, req) {
	var rss = grunt.file.readJSON("user/rss.json"),
		query = req.body,
		c = query.type,
		b = query.callback,
		i = query.id,
		u = query.username,
		d = query.userid;

	var day = (query.day || common.getTime(new Date().getTime(), "yyyyMMdd"));
	/*if (rss.data && rss.data[c] && rss.data[c].region === "cn") {
		fetchOtherServerJson(common.tmpl(otherserve_addgood, {
			type: c,
			id: i,
			username: u,
			userid: d
		}), c, function(result) {
			fetchToUserFile(c, day, i, result.data, u, d);
			res.send(b ? b + "(" + JSON.stringify({
				status: result.status,
				data: result.data
			}) + ")" : {
				status: result.status,
				data: result.data
			});
		});
	} else if (rss.data && rss.data[c] && rss.data[c].region === "en") {*/
		dir = "rss" + day;
		console.log(query, (new Date().getTime() - parseInt(query.key)) / 1000, 60);
		var filepath = path.resolve(dir + "/" + c + ".json");
		var iscity = false,
			city = "beijing";

		/*if (!grunt.file.exists(filepath)) {
			var pinyinlite = require('pinyinlite');
			city = pinyinlite(c).join('').split(',')[0];
			console.log(city)
			filepath = path.resolve(dir + "/baiducity_" + city + ".json");
			iscity = true;
		}*/

		var a = grunt.file.readJSON(filepath);
		console.log(filepath, !a.data[i] , hasUser(u, d))
		if (a && a.data && a.data[i] && hasUser(u, d)) {
			if (a.data[i].goodusers) {
				a.data[i].goodusers.push({
					gooduser_name: u,
					gooduser_id: d
				})
			} else {
				a.data[i].goodusers = [{
					gooduser_name: u,
					gooduser_id: d
				}];
			}
			if (a.data[i].goodcount) {
				a.data[i].goodcount += 1
			} else {
				a.data[i].goodcount = 1
			}
			common.extend(a.data[i], {
				goodcount: a.data[i].goodcount,
				goodusers: a.data[i].goodusers,
				lastgoodtime: (new Date()).getTime()
			})

			//if (!iscity) {
				//if (cache.get(day)[c]) {
				//	console.log("default")
				//	cache.get(day)[c].data[i] = a.data[i]
				//}
			//} else {
				//if (cache.get(day)["baiducity"] && cache.get(day)["baiducity"][city]) {
					//console.log("city")
					//cache.get(day)["baiducity"][city].data[i] = a.data[i];
				//}
			//}
			//console.log(cache.get(day))

			grunt.file.write(filepath, JSON.stringify(a));
			fetchToUserFile(c, day, i, a.data[i], u, d);
			fetchToUserMessageFile(c, day, i, a.data[i], u, d, false, " 赞了新闻 \"" + a.data[i].title + "\".");
			res.send(b ? b + "(" + JSON.stringify({
				status: 1,
				data: a.data[i]
			}) + ")" : {
				status: 1,
				data: a.data[i]
			});
		} else {
			res.send(b ? b + "(" + JSON.stringify({
				status: 0,
				data: []
			}) + ")" : {
				status: 0,
				data: []
			});
		}
	//}
};

exports.citynews = function(res, query) {
	var pinyinlite = require('pinyinlite');
	var a = grunt.file.readJSON("user/rss.json"),
		b = query.callback,
		c = "baiducity",
		l = 100,
		city = pinyinlite(query.city).join('').split(',')[0] || "fushun";

	var day = (query.day || common.getTime(new Date(), "yyyyMMdd"));
	dir = "rss" + day;
	console.log(query, (new Date().getTime() - parseInt(query.key)) / 1000, 60);

	var bool = (!query.key || query.key === "null" || query.key && (new Date().getTime() - parseInt(query.key)) / 1000 > 60) ? true : false;
	if (bool) {
		startTime = (new Date().getTime());
	}

	var isNow = common.getTime(new Date(), "yyyyMMdd") === day;
	console.log(a.data[c])
	a.data[c] ? fetchCity(a.data[c].link + city, a.data[c].typeId, c, city, day, false, a.data[c].charset || "utf-8", function(txt, type) {
		txt = txt.data || txt;
		var push = [];
		for (var i = 0; i < l; i++) txt[i] && push.push(txt[i]);
		res.send(b ? b + "(" + JSON.stringify({
			status: 1,
			data: push,
			total: txt.length,
			key: startTime,
			type: type
		}) + ")" : {
			status: 1,
			data: push,
			total: txt.length,
			key: startTime,
			type: type
		});
	}) : res.send(b ? b + "(" + JSON.stringify({
		status: 0,
		data: []
	}) + ")" : {
		status: 0,
		data: []
	});
}

function fetch(feed, typeId, from, day, bool, callback, fromtitle) {
	var posts, feedFrom = from;

	function done(err) {
		if (cache.has(day) && cache.get(day)[feedFrom]) {
			console.log("-> read cache. " + day + " - " + feedFrom);
			callback && callback(cache.get(day)[feedFrom], "cache");
		} else if (grunt.file.exists(path.resolve(dir + "/" + feedFrom + ".json"))) {
			console.log("-> read json file. " + feedFrom);
			posts = grunt.file.readJSON(path.resolve(dir + "/" + feedFrom + ".json"));
			cache.commit({
				[day]: {
					[feedFrom]: posts
				}
			});
			callback && callback(posts, "file");
		} else {
			callback(err);
		}
	}
	// Define our streams  
	if (cache.has(day) && cache.get(day)[feedFrom] && bool === false) {
		console.log("-> read cache. " + day + " - " + feedFrom);
		callback && callback(cache.get(day)[feedFrom], "cache");
	} else if (grunt.file.exists(path.resolve(dir + "/" + feedFrom + ".json")) && bool === false) {
		console.log("-> read json file. " + feedFrom);
		posts = grunt.file.readJSON(path.resolve(dir + "/" + feedFrom + ".json"));
		cache.commit({
			[day]: {
				[feedFrom]: posts
			}
		});
		callback && callback(posts, "file");
	} else {
		var req = request(feed, {
			timeout: 10000,
			pool: false
		});
		req.setMaxListeners(50);
		// Some feeds do not response without user-agent and accept headers.  
		req.setHeader('user-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36');
		req.setHeader('accept', 'text/html,application/xhtml+xml');

		var feedparser = new FeedParser();

		// Define our handlers  
		req.on('error', done);
		req.on('response', function(res) {
			console.log("-> response, " + feed);
			var stream = this,
				iconv, charset;
			posts = new Array();
			cache.commit({
				[day]: {
					[feedFrom]: posts
				}
			});
			if (res.statusCode != 200) return this.emit('error', new Error('Bad status code'));
			//console.log(res.headers['content-type']);
			charset = "utf-8";
			if (!iconv && charset && !/utf-*8/i.test(charset)) {
				try {
					iconv = new Iconv(charset, 'utf-8');
					iconv.on('error', done);
					stream = this.pipe(iconv);
				} catch (err) {
					this.emit('iconv error', err);
				}
			}
			stream.pipe(feedparser);
		});

		feedparser.on('error', done);
		feedparser.on('end', function(err) {
			new Promise((a, b) => {
				if (grunt.file.exists(dir + "/" + feedFrom + ".json")){
					var old = grunt.file.readJSON(dir + "/" + feedFrom + ".json");

					clonePostsData = [].concat(posts).concat(old.data);

					for (let i=0, len=clonePostsData.length; i<len; i++) {
				        for (let j=i+1; j<len; j++) {
				            if (clonePostsData[i].title == clonePostsData[j].title) {
				                clonePostsData.splice(j, 1);
				                len--;
				                j--;
				            }
				        }
				    }

				    posts = clonePostsData;
					console.log(posts.length)
					a(posts)
				}else{
					a(posts)
				}
			}).then((posts) => {
				console.log("-> read net file. " + feedFrom);
				var content = JSON.stringify(posts);
				console.log(path.resolve(dir + "/" + feedFrom + ".json"))
				grunt.file.write(path.resolve(dir + "/" + feedFrom + ".json"), "{\"data\":" + content + ",\"from\":\""+fromtitle+"\" }");
				callback && callback(posts, "net");
			}, (err) => {

			})
		});
		feedparser.on('readable', function() {
			var post;
			while (post = this.read()) {
				posts.push(transToPost(post)); //保存到对象数组  
			}
		});
	}

	function transToPost(post) {
		//var seg = new Segment();
		var title = (post.title + "").replace(/\r|\n/gim, " ").replace(/\s+/gim, " "),
			desc = (post.description + "").replace(/\r|\n/gim, " ").replace(/\s+/gim, " ").replace(/<|>/gim, function(a, b) {
				a = encodeURIComponent(a);
				return a;
			});
		if (desc.replace("...", "") === "") {
			desc = title + "...";
		}

		var result = {
			title: title,
			link: (post.url || post.link || post.alink || "") + "",
			description: desc,
			pubDate: (post.pubDate + "").replace(/\r|\n/gim, " ").replace(/\s+/gim, " "),
			source: /null/.test(post.source) ? feedFrom : (post.source || feedFrom),
			author: (post.author + "").replace(/\r|\n/gim, " ").replace(/\s+/gim, " "),
			typeId: typeId + "",
			fromtitle: fromtitle,
			day: day.substr(0, 4)+'-'+day.substr(4,2)+"-"+day.substr(6,2)
		};

		if (/\.png|\.jpg|\.gif/.test(desc)) {
			var src = [];
			desc.replace(/['"]*(http[s]*\:\/\/[a-zA-Z0-9\.\-\/]+\.(png|jpg|gif|jpeg)+)['"]*\s*/gim, function(a, b){
				src.push(b);
				//console.log(b)
				return a;
			})
			common.extend(result, {
				imgs: src
			});
			/*var src = /(http[s]*\:\/\/.+\.[png|jpg|gif|jpeg]+)/.exec(desc);
				console.log(src)
			if (src && src.length > 1) {
				common.extend(result, {
					img: src[1]
				});
			}*/
		}else{
			common.extend(result, {
				imgs: []
			});
		}

		return result;
	}

}

function fetchCity(feed, typeId, from, city, day, bool, local_charset, callback) {
	var posts, feedFrom = from;

	var pinyinlite = require('pinyinlite');
	city = pinyinlite(city.replace(/省|市|区|县|乡|镇|村/gim, "")).join('').split(',')[0];
	console.log(city)

	function done(err) {
		if (cache.has(day) && cache.get(day)[feedFrom] && cache.get(day)[feedFrom][city]) {
			console.log("-> read cache. " + day + " - " + feedFrom + " - " + city);
			callback && callback(cache.get(day)[feedFrom][city], "cache");
		} else if (grunt.file.exists(path.resolve(dir + "/" + feedFrom + "_" + city + ".json"))) {
			console.log("-> read json file. " + feedFrom);
			posts = grunt.file.readJSON(path.resolve(dir + "/" + feedFrom + "_" + city + ".json"));
			cache.commit({
				[day]: {
					[feedFrom]: {
						[city]: posts
					}
				}
			});
			callback && callback(posts, "file");
		} else {
			callback(err);
		}
	}
	// Define our streams  
	if (cache.has(day) && cache.get(day)[feedFrom] && cache.get(day)[feedFrom][city]) {
		console.log("-> read cache. " + day + " - " + feedFrom + " - " + city);
		callback && callback(cache.get(day)[feedFrom][city], "cache");
	} else if (grunt.file.exists(path.resolve(dir + "/" + feedFrom + "_" + city + ".json"))) {
		console.log("-> read json file. " + feedFrom);
		posts = grunt.file.readJSON(path.resolve(dir + "/" + feedFrom + "_" + city + ".json"));
		cache.commit({
			[day]: {
				[feedFrom]: {
					[city]: posts
				}
			}
		});
		callback && callback(posts, "file");
	} else {
		console.log(feed);
		var req = request(feed.replace(/省|市|区|县|乡|镇|村/gim, ""), {
			timeout: 10000,
			pool: false
		});
		req.setMaxListeners(50);
		// Some feeds do not response without user-agent and accept headers.  
		req.setHeader('user-agent', 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/61.0.3163.100 Mobile/13B143 Safari/601.1.46');
		req.setHeader('accept', 'text/html,application/xhtml+xml');

		var feedparser = new FeedParser();

		// Define our handlers  
		req.on('error', done);
		req.on('response', function(res) {
			console.log("-> response, " + feed);
			var stream = this,
				iconv, charset;
			posts = new Array();
			cache.commit({
				[day]: {
					[feedFrom]: {
						[city]: posts
					}
				}
			});
			if (res.statusCode != 200) return this.emit('error', new Error('Bad status code'));
			//console.log(res.headers['content-type']);
			charset = local_charset || "utf-8";
			if (!/utf-*8/i.test(charset)) {
				console.log("iconv")
				try {
					iconv = Iconv.decodeStream(charset);
					iconv.on('error', done);
					stream = this.pipe(iconv);
				} catch (err) {
					console.log(err)
				}
			}
			stream.pipe(feedparser);
		});

		feedparser.on('error', done);
		feedparser.on('end', function(err) {
			console.log("-> read net file. " + feedFrom);
			var content = stringify(posts);
			console.log(path.resolve(dir + "/" + feedFrom + "_" + city + ".json"))
			grunt.file.write(path.resolve(dir + "/" + feedFrom + "_" + city + ".json"), "{\"data\":" + content + "}");
			callback && callback(posts, "net");
		});
		feedparser.on('readable', function() {
			var post;
			while (post = this.read()) {
				posts.push(transToPost(post)); //保存到对象数组  
			}
		});
	}

	function transToPost(post) {
		return {
			title: (post.title + "").replace(/\r|\n/gim, " ").replace(/\s+/gim, " "),
			link: (post.url || post.link || post.alink || "") + "",
			description: (post.description + "").replace(/\r|\n/gim, " ").replace(/\s+/gim, " "),
			pubDate: (post.pubDate + "").replace(/\r|\n/gim, " ").replace(/\s+/gim, " "),
			source: post.source || "",
			author: (post.author + "").replace(/\r|\n/gim, " ").replace(/\s+/gim, " "),
			typeId: typeId + ""
		};
	}

}

function fetchWeb(res, req, feed, typeId, from, day, bool, callback) {
	var posts, feedFrom = from;

	function done(err) {
		if (cache.has(day) && cache.get(day)[feedFrom]) {
			console.log("-> read cache. " + day + " - " + feedFrom);
			callback(cache.get(day)[feedFrom], "cache");
		} else if (grunt.file.exists(path.resolve(dir + "/" + feedFrom + ".json"))) {
			console.log("-> read json file. " + feedFrom);
			posts = grunt.file.readJSON(path.resolve(dir + "/" + feedFrom + ".json"));
			cache.commit({
				[day]: {
					[feedFrom]: posts.data
				}
			});
			callback(posts, "file");
		} else {
			callback(err);
		}
	}
	// Define our streams  
	if (cache.has(day) && cache.get(day)[feedFrom]) {
		console.log("-> read cache. " + day + " - " + feedFrom);
		callback(cache.get(day)[feedFrom], "cache");
	} else if (grunt.file.exists(path.resolve(dir + "/" + feedFrom + ".json"))) {
		console.log("-> read json file. " + feedFrom);
		posts = grunt.file.readJSON(path.resolve(dir + "/" + feedFrom + ".json"));
		cache.commit({
			[day]: {
				[feedFrom]: posts.data
			}
		});
		callback(posts, "file");
	} else {
		console.log("fetchWeb ->");
		var fetchWebFn = require('node-fetch');
		var cheerio = require('cheerio');
		var url = require('url');

		res.header('Content-Type', 'application/xml; charset=utf-8');

		fetchWebFn(feed).then(
			response => response.text()
		).then((data) => {
			var $ = cheerio.load(data, {
				decodeEntities: false
			});
			var wbs = [];
			var items = $('.wgtCell');
			var wb, item, titleEle;
			items.map(function(index, ele) {
				wb = {};
				item = $(this);
				titleEle = item.find('.wgtCell_txt');
				wb.title = titleEle.text().replace(/^\s+|\s+$/g, '');
				if (wb.title.length > 24) {
					wb.title = wb.title.slice(0, 24) + '...';
				}
				wb.description = titleEle.html().replace(/\s+/g, ' ').replace(/thumbnail/, 'large').replace(/\r|\n/gim, " ");
				wb.pubDate = common.getTime(item.find('.link_d').html());
				wb.link = item.find('.wgtCell_tm a').attr('href');
				wbs.push(wb);
			});

			var posts = wbs;
			cache.commit({
				[day]: {
					[feedFrom]: wbs
				}
			});
			console.log("-> fetchWeb: read net file. " + feedFrom);
			var content = common.stringify(posts);
			console.log(path.resolve(dir + "/" + feedFrom + ".json"))
			grunt.file.write(path.resolve(dir + "/" + feedFrom + ".json"), "{\"data\":" + content + "}");
			callback(posts, "net");
		}).catch(function(error) {
			callback(error);
		});
	}
}

exports.getotherjson = function(res, query) {
	var a = grunt.file.readJSON("user/rss.json"),
		b = query.callback,
		c = query.channel || "baidu",
		l = query.count || 10;

	var day = (query.day || common.getTime(new Date(), "yyyyMMdd"));
	dir = "rss" + day;
	console.log(query, (new Date().getTime() - parseInt(query.key)) / 1000, 60);

	var bool = (!query.key || query.key === "null" || query.key && (new Date().getTime() - parseInt(query.key)) / 1000 > 60) ? true : false;
	if (bool) {
		startTime = (new Date().getTime());
	}

	fetchOtherJson(a.data[c].link, a.data[c].typeid, c, day, false, "utf-8", function(txt, type) {
		res.send({
			status: 1,
			data: txt.data || txt
		});
	});
};

function fetchOtherJson(feed, typeid, from, day, bool, local_charset, callback) {
	request(feed + (new Date()).getTime(), function(error, response, body) {
		if (!error && response.statusCode == 200) {
			var local_json = grunt.file.exists(path.resolve(dir + "/" + from + ".json")) && grunt.file.readJSON(path.resolve(dir + "/" + from + ".json")) || {
				data: []
			};
			var json = JSON.parse(body);
			for (var n in json.data) {
				local_json.data.push(json.data[n]);
			}
			json.data && grunt.file.write(path.resolve(dir + "/" + from + ".json"), "{\"data\":" + JSON.stringify(local_json.data) + "}");
			callback(local_json);
		}
	})
}

function sendMail(email, userinfo, key, title, webTmpl) {
	var nodemailer = require('nodemailer');
	var transporter = nodemailer.createTransport({
		//https://github.com/andris9/nodemailer-wellknown#supported-services 支持列表
		service: 'qq',
		port: 465, // SMTP 端口
		secureConnection: true, // 使用 SSL
		auth: {
			user: '18900360@qq.com',
			//这里密码不是qq密码，是你设置的smtp密码
			pass: 'xwffhknnjotkbgfa'
		}
	});

	// NB! No need to recreate the transporter object. You can use
	// the same transporter object for all e-mails

	// setup e-mail data with unicode symbols
	var mailOptions = {
		from: '18900360@qq.com', // 发件地址
		to: email, // 收件列表
		subject: title ? title : 'Forgot password', // 标题
		//text和html两者只支持一种
		text: title ? title : 'Forgot password', // 标题
		html: webTmpl ? webTmpl : 'Click link,<a href="http://reg.iliulan.com/?channel=20170003&fpt=resetpassword&email=' + userinfo.email + '&k=' + key + '" target="_blank">Reset password</a>.' // html 内容
	};

	// send mail with defined transport object
	transporter.sendMail(mailOptions, function(error, info) {
		if (error) {
			return console.log(error);
		}
		console.log('Message sent: ' + info.response);

	});
}

function getCookieUserInfo(req) {
	if (!req.cookies.userToken) {
		return null;
	}

	var signin = decodeURIComponent(decodeURIComponent(req.cookies.userToken)).split(',');

	var ua = signin[1].split('=')[1],
		ud = signin[0].split('=')[1],
		uk = signin[2].split('=')[1],
		ui = signin[4] && signin[4].split('=')[1] || "";

	return {
		username: ua,
		userid: ud,
		email: uk,
		userico: ui
	}
}

exports.getlinkedusers = function(res, query) {
	var a = grunt.file.readJSON("user/__userdata.json"),
		b = query.callback,
		c = query.channel || "baidu",
		id = query.id || 0,
		d = query.day || common.getTime(new Date(), "yyyyMMdd");

	//console.log(query)
	getchannelnews(res, query, function(txt) {
		var data = txt.data || txt,
			user_group = [];
		console.log(data[parseInt(id)], id)
		if (data && data[id]) {
			var users = data[id].goodusers;
			if (users && users.length > 0) {
				//console.log(users)
				for (var i = 0; i < users.length; i++) {
					//console.log(users[i])
					for (var n in a.users) {
						//console.log(a.users[n])
						if (a.users[n].name === users[i].gooduser_name && parseInt(a.users[n].id) === parseInt(users[i].gooduser_id)) {
							user_group.push(users[i]);
							break;
						}
					}
				}
				res.send(b ? b + "(" + JSON.stringify({
					status: 1,
					data: user_group
				}) + ")" : {
					status: 1,
					data: user_group
				});
			} else {
				api_error(res, query, false, "no liked");
			}
		} else {
			api_error(res, query, false, "no news");
		}
	});
};

function hasArray(arr, key) {
	var bool = false;
	if (arr.length === 0) {
		return bool;
	}
	for (var i = 0; i < arr.length; i++) {
		if (arr[i].username === key.username && parseInt(arr[i].userid) === parseInt(key.userid)) {
			bool = true;
			break;
		}
	}
	return bool;
}

exports.addfollow = function(res, req) {
	var a = grunt.file.readJSON("user/__userfollows.json"),
		query = req.body,
		b = query.callback,
		u = query.followusername,
		d = query.followuserid,
		cu = query.username,
		cd = query.userid,
		ce = query.email;

	/*var usinfo = getCookieUserInfo(req);

	if (!usinfo) {
		api_error(res, query, false, "Not logged in");
		return;
	}*/

	var ua = cu,
		ud = cd,
		uk = ce;

	console.log(ua, ud, uk)

	if (a && a.users && hasUser(u, d) && a.users[uk] && a.users[uk].name === ua && a.users[uk].id === parseInt(ud)) {
		if (!a.users[uk].follows) a.users[uk].follows = [];
		var addfr = 0,
			addfs = 0;
		for (var n in a.users) {
			if (a.users[n].name === u && parseInt(a.users[n].id) === parseInt(d)) {
				console.log(a.users[n].name, a.users[n].id)
				if (!a.users[n].follower) a.users[n].follower = [];
				addfr = hasArray(a.users[n].follower, {
					username: a.users[uk].name,
					userid: a.users[uk].id
				});
				if (!addfr) {
					a.users[n].follower.push({
						username: a.users[uk].name,
						userid: a.users[uk].id,
						userico: a.users[uk].userico || ""
					});
					console.log(a.users[n].follower)
					addfr = true;
				}
				addfs = hasArray(a.users[uk].follows, {
					username: u,
					userid: d
				});
				if (!addfs) {
					a.users[uk].follows.push({
						username: u,
						userid: d,
						userico: a.users[n].userico || ""
					});
					console.log(a.users[uk].follows)
					addfs = true;
					fetchToUserMessageFile(null, "", 0, {
						"username": u,
						"userid": d,
						"userico": ""
					}, cu, cd, false, " 关注了 " + u + ".");
					/*if (!a.users[uk].message) a.users[uk].message = [];
					a.users[uk].message.push({
						message: ua + " followed " + u + ".",
						time: common.getTime(new Date(), "yyyy-MM-dd hh:mm:ss")
					});*/
				}

				break;
			}
		}

		//console.log(a.users[uk].follows)

		console.log(addfr, addfs)
		if (addfs === true) {
			grunt.file.write("user/__userfollows.json", JSON.stringify(a));
			res.send(b ? b + "(" + JSON.stringify({
				status: 1
			}) + ")" : {
				status: 1
			});
		} else {
			api_error(res, query, false, "add follow error");
		}
	} else {
		api_error(res, query, false, "Unknown user");
	}
};

exports.delfollow = function(res, req) {
	var a = grunt.file.readJSON("user/__userfollows.json"),
		query = req.body,
		b = query.callback,
		u = query.followusername,
		d = query.followuserid,
		username = query.username,
		userid = query.userid,
		email = query.email;

	/*var usinfo = getCookieUserInfo(req);

	if (!usinfo) {
		api_error(res, query, false, "Not logged in");
		return;
	}*/

	var ua = username,
		ud = userid,
		uk = email;

	//console.log(hasUser(u, d), u, d)

	if (a && a.users && hasUser(u, d) && a.users[uk] && a.users[uk].name === ua && a.users[uk].id === parseInt(ud)) {
		if (a.users[uk].follows && a.users[uk].follows.length > 0) {
			var follows = a.users[uk].follows;
			for (var i = 0; i < follows.length; i++) {
				//console.log(follows[i].userid === parseInt(d), follows[i].username === u)
				if (parseInt(follows[i].userid) === parseInt(d) && follows[i].username === u) {
					follows.splice(i, 1);
					break;
				}
			}

			a.users[uk].follows = follows;

			for (var n in a.users) {
				if (a.users[n].name === u && parseInt(a.users[n].id) === parseInt(d)) {
					if (!a.users[n].follower) {
						a.users[n].follower = [];
						break;
					}
					var follower = a.users[n].follower;
					for (var i = 0; i < follower.length; i++) {
						if (parseInt(follower[i].userid) === parseInt(d) && follower[i].username === u) {
							follower.splice(i, 1);
							break;
						}
					}
					a.users[n].follower = follower;
					break;
				}
			}
			//console.log(follows)
			fetchToUserMessageFile(null, "", 0, {
				"username": u,
				"userid": d,
				"userico": ""
			}, ua, ud, false, " 取消关注了 " + u + ".");

			grunt.file.write("user/__userfollows.json", JSON.stringify(a));
			res.send(b ? b + "(" + JSON.stringify({
				status: 1
			}) + ")" : {
				status: 1
			});
		} else {
			api_error(res, query, false, "no follows");
		}
	} else {
		api_error(res, query, false, "Unknown user");
	}
};

exports.getfollowusers = function(res, req) {
	var a = grunt.file.readJSON("user/__userfollows.json"),
		query = req.body,
		b = query.callback,
		i = query.userid,
		users = [];

	if (a && a.users) {
		for (var n in a.users) {
			if (parseInt(a.users[n].id) === parseInt(i)) {
				users = a.users[n].follows || [];
				break;
			}
		}
		res.send(b ? b + "(" + JSON.stringify({
			status: 1,
			data: users
		}) + ")" : {
			status: 1,
			data: users
		});
	} else {
		api_error(res, query);
	}
};

function isMutualConcern(aid, bid, data){
	var bool = false;
	for(var n in data){
		if (parseFloat(data[n].id) === parseFloat(aid)){
			for (var i=0;i<data[n].follows.length;i++){
				if (parseFloat(data[n].follows[i].userid) === parseFloat(bid)){
					bool = true;
					break;
				}
			}
			if (bool) break;
		}
	}
	return bool;
}

exports.getfollowerusers = function(res, req) {
	var a = grunt.file.readJSON("user/__userfollows.json"),
		query = req.body,
		b = query.callback,
		i = query.userid,
		users = [];

	if (a && a.users) {
		for (var n in a.users) {
			if (parseInt(a.users[n].id) === parseInt(i)) {
				users = a.users[n].follower || [];
				break;
			}
		}
		for (var x=0;x<users.length;x++){
			var result = isMutualConcern(users[x].userid, i, a.users);
			if (result){
				users[x] = common.extend(users[x], {
					isMutualConcern: true
				});
			}
		}

		res.send(b ? b + "(" + JSON.stringify({
			status: 1,
			data: users
		}) + ")" : {
			status: 1,
			data: users
		});
	} else {
		api_error(res, query);
	}
};

exports.getusergoods = function(res, req) {
	var a = grunt.file.readJSON("user/__usergoods.json"),
		query = req.body,
		b = query.callback,
		i = query.userid,
		goods = [];

	if (a && a.users) {
		for (var n in a.users) {
			if (parseInt(a.users[n].id) === parseInt(i)) {
				goods = a.users[n].goodsArticle || [];
				break;
			}
		}
		res.send(b ? b + "(" + JSON.stringify({
			status: 1,
			data: goods
		}) + ")" : {
			status: 1,
			data: goods
		});
	} else {
		api_error(res, query);
	}
};

function hasFollow(follow, id) {
	for (var i = 0; i < follow.length; i++) {
		if (parseInt(follow[i].userid) === parseInt(id)) {
			id = 2;
			break;
		}
	}
	return id !== 2 ? 1 : 2;
}

exports.isfollow = function(res, req) {
	var a = grunt.file.readJSON("user/__userfollows.json"),
		query = req.body,
		b = query.callback,
		ids = query.ids,
		username = query.username,
		userid = query.userid,
		email = query.email;

	/*var usinfo = getCookieUserInfo(req);

	if (!usinfo) {
		api_error(res, query, false, "Not logged in");
		return;
	}*/

	var ua = username,
		ud = userid,
		uk = email;

	if (a && a.users[uk] && a.users[uk].follows) {
		var follow = a.users[uk].follows;
		console.log(follow);
		ids = ids.split('|');
		if (ids.length===1){
			const bool = hasFollow(follow, ids[0]);
			if (bool===1){
				res.send(b ? b + "(" + JSON.stringify({
					status: 0
				}) + ")" : {
					status: 0
				});
			}else{
				res.send(b ? b + "(" + JSON.stringify({
					status: 1
				}) + ")" : {
					status: 1
				});
			}
			return;
		}
		for (var x = 0; x < ids.length; x++) {
			if (!follow || follow.length === 0) {
				ids[x] = 1;
			} else {
				ids[x] = hasFollow(follow, ids[x]);
				/*for (var i = 0; i < follow.length; i++) {
					console.log(parseInt(follow[i].userid), parseInt(ids[x]))
					if (parseInt(follow[i].userid) === parseInt(ids[x])) {
						ids[x] = 2;
					} else {
						ids[x] = 1;
					}
				}*/
			}
		}
		console.log(ids)
		res.send(b ? b + "(" + JSON.stringify({
			status: 1
		}) + ")" : {
			status: 1
		});
	} else {
		api_error(res, query);
	}
};

exports.usermessage = function(res, req) {
	var a = grunt.file.readJSON("user/__usermessage.json"),
		query = req.body,
		b = query.callback,
		username = query.username;
		userid = query.userid;
		email = query.email;

	/*var usinfo = getCookieUserInfo(req);

	if (!usinfo) {
		api_error(res, query, false, "Not logged in");
		return;
	}*/

	var ua = username,
		ud = userid,
		uk = email;

	if (userid && parseInt(userid) === parseInt(ud) && hasUser(ua, ud) && a && a.users) {
		var message = null;
		for (var i in a.users) {
			if (parseInt(a.users[i].id) === parseInt(userid)) {
				if (!a.users[i].message) {
					api_error(res, query, false, "no message");
					return;
					break;
				}
				message = a.users[i].message;
				break;
			}
		}
		if (message) {
			//grunt.file.write("user/__userdata.json", JSON.stringify(a));
			res.send(b ? b + "(" + JSON.stringify({
				status: 1,
				data: message
			}) + ")" : {
				status: 1,
				data: message
			});
		} else {
			api_error(res, query, false, "no message");
		}

	} else {
		api_error(res, query, false, "no user");
	}
}

exports.getfollowermessage = function(res, req) {
	var a = grunt.file.readJSON("user/__usermessage.json"),
		c = grunt.file.readJSON("user/__userfollows.json"),
		query = req.body,
		username = query.username,
		userid = query.userid,
		email = query.email,
		b = query.callback;	

	/*var usinfo = getCookieUserInfo(req);

	if (!usinfo) {
		api_error(res, query, false, "Not logged in");
		return;
	}*/

	var ua = username,
		ud = userid,
		uk = email;

	if (hasUser(ua, ud) && a && a.users) {
		console.log(query)
		var f = c.users[uk].follows,
			message = [],
			len = 20;
		if (f.length > 1) len = Math.round(len/f.length);
		console.log(len)
		for (var i = 0; i < f.length; i++) {
			for (var n in a.users) {
				if (f[i].username === a.users[n].name && parseInt(f[i].userid) === parseInt(a.users[n].id)) {
					if (a.users[n].message) {
						for (var x = 0; x < a.users[n].message.length; x++) {
							//console.log(new Date().getTime(),new Date(a.users[n].message[x].time).getTime())
							if (a.users[n].message[x]) {
								if (message.length > 0 && new Date(message[0].time) < new Date(a.users[n].message[x].time)) {
									message.splice(0, 0, common.extend(a.users[n].message[x], {
										username: a.users[n].name,
										userid: a.users[n].id
									}));
								} else {
									message.push(common.extend(a.users[n].message[x], {
										username: a.users[n].name,
										userid: a.users[n].id
									}));
								}
								if (x+1>len) break;
							}
						}
					}
				}
			}
		}
		if (message.length > 0) {
			res.send(b ? b + "(" + JSON.stringify({
				status: 1,
				data: message
			}) + ")" : {
				status: 1,
				data: message
			});
		} else {
			api_error(res, query, false, "no message");
		}
	} else {
		api_error(res, query, false, "no user");
	}
}

exports.push = function(ws, req) {
	ws.on('message', function(msg) {
		console.log(msg);
		ws.send(msg)
	})
	console.log(req)
};

var AipSpeechClient = require("baidu-aip-sdk").speech;

// 设置APPID/AK/SK
var APP_ID = "16819296";
var API_KEY = "6UXSAbYcUUfzFbOcq7LqSNjS";
var SECRET_KEY = "60ylUSBWmwKB7sxKdpZIHN1Ne6A3lkii";

// 新建一个对象，建议只保存一个对象调用服务接口
var client = new AipSpeechClient(APP_ID, API_KEY, SECRET_KEY);

exports.aip = function(res, req) {
	var b = req.body.callback,
		message = "";

	console.log(req.files)
	let originalFilename = req.files.file.name;
	let extName = "";
	//判断文件是否具有扩展名
	if (originalFilename.indexOf(".") != -1) {
		extName = originalFilename.slice(originalFilename.indexOf(".")); //获取文件扩展名
	}
	let fileName = Math.round(Math.random() * 10000) + extName;
	let result = fs.createReadStream(req.files.file.path);
	var output = path.join(__dirname, "./public/uploads/", req.files.file.name);
	var dest = fs.createWriteStream(output);
	result.pipe(dest);
	result.on('end', function() {
		fs.unlinkSync(req.files.file.path);
		let voice = fs.readFileSync(output);
		let voiceBuffer = new Buffer(voice);
		// 识别本地文件
		client.recognize(voiceBuffer, 'wav', 16000, {dev_pid: '1536'}).then(function (a) {
		    console.log('<recognize>: ' + JSON.stringify(a));
			if (a.err_msg === "success.") {
				res.send({
					status: 1,
					data: a.result
				});
			}else{
				res.send({
					status: 0,
					data: message
				});
			}
		}, function(err) {
		    res.send({
				status: 0,
				data: message
			});
		});
	});
	result.on('error', function(err) {
		res.send({
			status: 0,
			data: message
		});
	});
}

exports.youdao = function(res, req) {
	let query = req.body,
		b = query.callback,
		key = query.key;

	var request = require('request');
	request('http://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i='+encodeURIComponent(key), function (error, response, body) {
	  if (!error && response.statusCode == 200) {
	    res.send(b ? b + "(" + JSON.stringify({
			status: 1,
			data: JSON.parse(body).translateResult
		}) + ")" : {
			status: 1,
			data: JSON.parse(body).translateResult
		});
	  }else if (error){
	  	console.log(error);
	  	res.send({
			status: 0,
			data: []
		});
	  }
	});

}

exports.videos = function(res, req){
	var query = req.body,
		a = grunt.file.readJSON("tmp/youku.json");

	res.send({
		status: 1,
		data: a.data
	});
}

exports.videoinfo = function(res, req){
	var query = req.body,
		id = query.id,
		a = grunt.file.readJSON("tmp/youku.json"),
		video = {};

	for (var i=0;i<a.data.length;i++){
		if (a.data[i].videoId === id){
			video = a.data[i];
			break;
		}
	}

	res.send({
		status: 1,
		data: video
	});
}

exports.fetchVideo = function(res, query){

	var fetchWebFn = require('node-fetch');
		var cheerio = require('cheerio');
		var url = require('url');

		res.header('Content-Type', 'application/xml; charset=utf-8');
		var content = [];
		function fetchData(v){
			if (v >= 6){
				done();
			}else{
				fetchWebFn("https://list.youku.com/category/page?c=96&g=&pt=1&s=4&u=&type=show&p="+v).then(
					response => response.text()
				).then((data) => {
					console.log(v)
					try{
						var json = JSON.parse(data);
						if (json.success === true){
							console.log(v + " success.")
							content = content.concat(json.data);
							fetchData(v+=1);
						}else{
							console.log(v + " error.1")
							fetchData(v+=1);
						}
					}catch(e){
						done();
					}
				}).catch(function(error) {
					done();
				});
			}
		}
		function done(){
			//v.youku.com/v_show/id_XMTczNTQ0MDIxNg
			content.map(function(video,i) {
				var id = video.videoLink.replace("//v.youku.com/v_show/id_", "").replace(".html", "");
				content[i] = common.extend(video, {
					videoplay: "http://player.youku.com/embed/"+id
				})
			})
			//<iframe height=498 width=510 src='http://player.youku.com/embed/XMTczNTQ0MDIxNg' frameborder=0 'allowfullscreen'></iframe>
			var len = content.length;
			content = JSON.stringify(content);
			console.log(path.resolve("tmp/youku.json"))
			grunt.file.write(path.resolve("tmp/youku.json"), "{\"data\":" + content + ", \"length\":"+len+"}");
			res.send({
				status: 1
			});
		}
		fetchData(1)
}

exports.fetchBaiduHotSearch = function(res, req){
	fetchBaiduHotSearchFn(res,req)
}

function fetchBaiduHotSearchFn(res, req){

	var fetchWebFn = require('node-fetch');
		var cheerio = require('cheerio');
		var url = require('url');

		res.header('Content-Type', 'application/xml; charset=utf-8');
		var content = [];
		function fetchData(v){
			if (v >= 6){
				done();
			}else{
				fetchWebFn("http://top.baidu.com/buzz?b=342&c=513&fr=topbuzz_b1_c513").then(
					response => response.text()
				).then((data) => {
					console.log(v)
					try{
						var $ = cheerio.load(data, {
							decodeEntities: false
						});
						var wbs = [];
						var items = $('a.list-title');
						var wb, item, titleEle;
						items.map(function(index, ele) {
							wb = false;
							item = $(this);
							titleEle = item;
							wb = titleEle.text().replace(/^\s+|\s+$/g, '');
							wb && wbs.push(wb);
						});

						content = wbs;
						done();
					}catch(e){
						done();
					}
				}).catch(function(error) {
					done();
				});
			}
		}
		function done(){
			var len = content.length;
			content = JSON.stringify(content);
			console.log(path.resolve("tmp/baidusearchhotkeys.json"))
			grunt.file.write(path.resolve("tmp/baidusearchhotkeys.json"), "{\"data\":" + content + ", \"length\":"+len+"}");
			res.send({
				status: 1
			});
		}
		fetchData(1)

}